package xml;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import phenix.com.phenixdemo.ActivityMain;
import phenix.com.phenixdemo.R;
import phenix.com.phenixdemo.control.ListViewService;
import phenix.com.phenixdemo.control.WidgetClickHandlerService;

/**
 * Implementation of App Widget functionality.
 */
public class AppWidgetPhenix extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget_phenix);
        views.setTextViewText(R.id.appwidget_text, widgetText);
        Intent intent  = new Intent(context,ActivityMain.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,1,intent,0);
        views.setOnClickPendingIntent(R.id.appwidget_text,pendingIntent);

        Intent intentService = new Intent(context, ListViewService.class);
        views.setRemoteAdapter(R.id.listView,intentService);
//注意,下面这段代码不能少,否则点击没有效果
        Intent clickIntent = new Intent(context, WidgetClickHandlerService.class);
        PendingIntent clickPendingIntent = PendingIntent.getService(
                context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.listView, clickPendingIntent);

// Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

